﻿namespace SimpleWebServices.Web.Mvc
{
    using System;
    using System.Web;
    using System.Web.Mvc;
    using Moq;
    using SimpleWebServices.Security.Cryptography;
    using Xunit;

    public sealed class StyleSheetResultFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<StyleSheetResult>()
                .DerivesFrom<ActionResult>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new StyleSheetResult());
        }

        [Fact]
        public void op_ExecuteResult_ControllerContextNull()
        {
            Assert.Throws<ArgumentNullException>(() => new StyleSheetResult().ExecuteResult(null as ControllerContext));
        }

        [Fact]
        public void op_ExecuteResult_ControllerContext()
        {
            string content = "@import \"http://static.example.com/screen.css\";";

            var mock = new Mock<HttpContextBase>();
            mock.Setup(x => x.Response.Cache.SetCacheability(HttpCacheability.Public)).Verifiable();
            mock.Setup(x => x.Response.Cache.SetETag(It.Is<string>(etag => etag == MD5Hash.Compute(content)))).Verifiable();
            mock.SetupSet(x => x.Response.ContentType).Callback(expected => Assert.Equal<string>("text/css", expected)).Verifiable();
            mock.Setup(x => x.Response.Write(It.Is<string>(s => s == content))).Verifiable();

            StyleSheetResult obj = new StyleSheetResult
            {
                Content = content
            };

            obj.ExecuteResult(new ControllerContext()
            {
                HttpContext = mock.Object
            });

            mock.VerifyAll();
        }

        [Fact]
        public void op_ExecuteResult_ControllerContext_whenContentNull()
        {
            var mock = new Mock<HttpContextBase>();
            mock.SetupSet(x => x.Response.ContentType).Callback(expected => Assert.Equal<string>("text/css", expected)).Verifiable();
            mock.Setup(x => x.Response.Write(It.Is<string>(s => s == null))).Verifiable();

            new StyleSheetResult().ExecuteResult(new ControllerContext()
            {
                HttpContext = mock.Object
            });

            mock.VerifyAll();
        }
    }
}